home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / share / hplip / fax / coverpages.py < prev    next >
Text File  |  2008-10-13  |  7KB  |  202 lines

  1. # -*- coding: utf-8 -*-
  2. #
  3. # (c) Copyright 2003-2007 Hewlett-Packard Development Company, L.P.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  18. #
  19. # Author: Don Welch
  20. #
  21.  
  22. from reportlab.platypus.paragraph import Paragraph
  23. from reportlab.platypus.flowables import Preformatted
  24. from reportlab.platypus.doctemplate import *
  25. #from reportlab.rl_config import TTFSearchPath
  26. from reportlab.platypus import SimpleDocTemplate, Spacer
  27. from reportlab.platypus.tables import Table, TableStyle
  28. from reportlab.lib.pagesizes import letter, legal, A4
  29. from reportlab.lib.units import inch
  30. from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet
  31. from reportlab.lib import colors
  32. #from reportlab.pdfbase import pdfmetrics
  33. #from reportlab.pdfbase.ttfonts import TTFont
  34. from time import localtime, strftime
  35. import warnings
  36. warnings.simplefilter('ignore', DeprecationWarning)
  37.  
  38. if __name__ ==  "__main__":
  39.     import sys
  40.     sys.path.append("..")
  41.     
  42. from base.g import *
  43. from base import utils
  44.  
  45. PAGE_SIZE_LETTER = 'letter'
  46. PAGE_SIZE_LEGAL = 'legal'
  47. PAGE_SIZE_A4 = 'a4'
  48.  
  49. def escape(s):
  50.     return s.replace("&", "&").replace(">", ">").replace("<", "<")
  51.     
  52.  
  53. def createStandardCoverPage(page_size=PAGE_SIZE_LETTER,
  54.                             total_pages=1, 
  55.                             recipient_name='', 
  56.                             recipient_phone='', 
  57.                             recipient_fax='', 
  58.                             sender_name='', 
  59.                             sender_phone='',
  60.                             sender_fax='', 
  61.                             sender_email='', 
  62.                             regarding='', 
  63.                             message='',
  64.                             preserve_formatting=False,
  65.                             output=None):
  66.  
  67.     s = getSampleStyleSheet()
  68.  
  69.     story = []
  70.  
  71.     #print prop.locale
  72.     #TTFSearchPath.append('/usr/share/fonts/truetype/arphic')
  73.     #pdfmetrics.registerFont(TTFont('UMing', 'uming.ttf'))
  74.  
  75.     ps = ParagraphStyle(name="title", 
  76.                         parent=None, 
  77.                         fontName='helvetica-bold',
  78.                         #fontName='STSong-Light',
  79.                         #fontName = 'UMing',
  80.                         fontSize=36,
  81.                         )
  82.  
  83.     story.append(Paragraph("Fax", ps))
  84.  
  85.     story.append(Spacer(1, inch))
  86.  
  87.     ps = ParagraphStyle(name='normal',
  88.                         fontName='Times-Roman',
  89.                         #fontName='STSong-Light',
  90.                         #fontName='UMing',
  91.                         fontSize=12) 
  92.  
  93.     recipient_name_label = Paragraph("To:", ps)
  94.     recipient_name_text = Paragraph(escape(recipient_name[:64]), ps)
  95.  
  96.     recipient_fax_label = Paragraph("Fax:", ps)
  97.     recipient_fax_text = Paragraph(escape(recipient_fax[:64]), ps)
  98.  
  99.     recipient_phone_label = Paragraph("Phone:", ps)
  100.     recipient_phone_text = Paragraph(escape(recipient_phone[:64]), ps)
  101.  
  102.  
  103.     sender_name_label = Paragraph("From:", ps)
  104.     sender_name_text = Paragraph(escape(sender_name[:64]), ps)
  105.  
  106.     sender_phone_label = Paragraph("Phone:", ps)
  107.     sender_phone_text = Paragraph(escape(sender_phone[:64]), ps)
  108.  
  109.     sender_email_label = Paragraph("Email:", ps)
  110.     sender_email_text = Paragraph(escape(sender_email[:64]), ps)
  111.  
  112.  
  113.     regarding_label = Paragraph("Regarding:", ps)
  114.     regarding_text = Paragraph(escape(regarding[:128]), ps)
  115.  
  116.     date_time_label = Paragraph("Date:", ps)
  117.     date_time_text = Paragraph(strftime("%a, %d %b %Y %H:%M:%S (%Z)", localtime()), ps)
  118.  
  119.     total_pages_label = Paragraph("Total Pages:", ps)
  120.     total_pages_text = Paragraph("%d" % total_pages, ps)
  121.  
  122.     data = [[recipient_name_label, recipient_name_text, sender_name_label, sender_name_text],
  123.             [recipient_fax_label, recipient_fax_text, sender_phone_label, sender_phone_text],
  124.             [date_time_label, date_time_text, sender_email_label, sender_email_text],
  125.             [regarding_label, regarding_text, total_pages_label, total_pages_text]]
  126.  
  127.     LIST_STYLE = TableStyle([('LINEABOVE', (0,0), (-1,0), 2, colors.black),
  128.                              ('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black),
  129.                              ('LINEBELOW', (0,-1), (-1,-1), 2, colors.black),
  130.                              ('ALIGN', (1,1), (-1,-1), 'RIGHT'),
  131.                              ('VALIGN', (0, 0), (-1, -1), 'TOP'), 
  132.                             ])
  133.  
  134.     t = Table(data, style=LIST_STYLE)
  135.  
  136.     story.append(t)
  137.  
  138.     if message:
  139.         MSG_STYLE = TableStyle([('LINEABOVE', (0,0), (-1,0), 2, colors.black),
  140.                                  ('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black),
  141.                                  ('LINEBELOW', (0,-1), (-1,-1), 2, colors.black),
  142.                                  ('ALIGN', (1,1), (-1,-1), 'RIGHT'),
  143.                                  ('VALIGN', (0, 0), (-1, -1), 'TOP'),
  144.                                  ('SPAN', (-2, 1), (-1, -1)), 
  145.                                 ])
  146.  
  147.         story.append(Spacer(1, 0.5*inch))
  148.  
  149.         if preserve_formatting:
  150.             message = '\n'.join(message[:2048].splitlines()[:32])
  151.             
  152.             data = [[Paragraph("Comments/Notes:", ps), ''],
  153.                     [Preformatted(escape(message), ps), ''],]
  154.         else:
  155.             data = [[Paragraph("Comments/Notes:", ps), ''],
  156.                     [Paragraph(escape(message[:2048]), ps), ''],]
  157.  
  158.         t = Table(data, style=MSG_STYLE)
  159.  
  160.         story.append(t)
  161.  
  162.     if page_size == PAGE_SIZE_LETTER:
  163.         pgsz = letter
  164.     elif page_size == PAGE_SIZE_LEGAL:
  165.         pgsz = legal
  166.     else:
  167.         pgsz = A4
  168.  
  169.     if output is None:
  170.         f_fd, f = utils.make_temp_file()
  171.     else:
  172.         f = output
  173.  
  174.     doc = SimpleDocTemplate(f, pagesize=pgsz)
  175.     doc.build(story)
  176.  
  177.     return f
  178.  
  179.  
  180.  
  181. #            { "name" : (function, "thumbnail.png"), ... }    
  182. COVERPAGES = { "basic": (createStandardCoverPage, 'standard_coverpage.png'),
  183.              }
  184.  
  185.              
  186. if __name__ ==  "__main__":
  187.     createStandardCoverPage(page_size=PAGE_SIZE_LETTER,
  188.                                 total_pages=1, 
  189.                                 recipient_name='µ│òσ¢╜', 
  190.                                 recipient_phone='1234', 
  191.                                 recipient_fax='4321', 
  192.                                 sender_name='Don', 
  193.                                 sender_phone='1234',
  194.                                 sender_fax='5678', 
  195.                                 sender_email='test@hplip.sf.net', 
  196.                                 regarding='Test', 
  197.                                 message='Message',
  198.                                 preserve_formatting=False,
  199.                                 output="output.pdf")
  200.                                 
  201.  
  202.